
cc.Class({
    extends: cc.Component,

    properties: {
        score : cc.Label,
        gate :cc.Label,
        bomb : cc.Label,
        bombBtn : cc.Node,
        friendlyLinksNode:[cc.Node],
        playerBlood:cc.ProgressBar,
        supCount:cc.Label,
        baozou:cc.Node,
        hudun:cc.Node,
        liaoji:cc.Node,
        weili:cc.Node,
        farms:[cc.SpriteFrame],
        moveProp:cc.Node,
        earlyWarn : require('UI-Toggle'),
        waring:cc.Node,
        tips : cc.Node,
        mask:cc.Node,
        bombNode:cc.Node,
        loadSF : [cc.SpriteFrame],
        fire:cc.Node
    },

    onLoad () {
        
        this.fire.active=false;
        this.mask.active=false
        global.luck=true;
        this.useSup=0;
        if(global.gates!=0){
            this.gate.string='第'+(parseInt(global.gates.split('-')[0])*4-4+parseInt(global.gates.split('-')[1]))+'关';
        }
        if(global.gates==0){
            this.gate.string='无尽出击'
        }else if(global.gates==-1){
            this.gate.string='新手引导'
        }
        global.Event.on('upBullet',this.onEventUpBullet.bind(this),this);
        global.Event.on('crazyTime',this.onEventCrazyTime.bind(this),this);

        global.Event.on('bossBattle',this.onEventBossBattle.bind(this),this);
        global.Event.on('bossBlood',this.onEventUpdateBossBlood.bind(this),this);
        global.Event.on('playerBlood',this.onEventUpdatePlayerBlood.bind(this),this);
        global.Event.on('onEventBassExit',this.onEventBassExit.bind(this),this);
        global.Event.on('LUCK',this.luck.bind(this),this);
        global.Event.on('SHOWBLOOD',this.showBlood.bind(this),this);
        global.Event.on('bombhint',function(){
            // this.bombNode.runAction(cc.repeat(cc.sequence(cc.fadeTo(0.3,100),cc.fadeTo(0.3,255)),5));
            this.fire.active=true;
            this.scheduleOnce(()=>{
                this.fire.active=false;
            },3)
        }.bind(this));
        
        // global.Event.on('GETPROP',this.addPropTips.bind(this),this)
        // this.onEventUpBullet(global.Data.skillLevel)
        this.schedule(this.updateRunTime,1.0);
        this.node.getChildByName('bossInfo').active = false;
        this.onEventUpBullet(3);
        // if(global.Data.isFirst==undefined || global.Data.isFirst==true){
        //     this.scheduleOnce(()=>{
        //         global.UIMgr.addUI('game/GameLearn')
        //     },2)
        // };
        // this.checkHaveFriendlyLink();
        // this.setProgress();
        this.earlyWarn.node.active = false
        this.waring.active=false;
        
        // cc.game.on(cc.game.EVENT_HIDE, function () {
        //     console.log('暂停')
        //     cc.director.pause()
        // });
        // cc.game.on(cc.game.EVENT_SHOW, function () {
        //     console.log('恢复')
        //     cc.director.resume()
        // });
    },
    

    setProgress(){
        this.playerBlood.progress=0;
        this.schedule(function(){
            this.playerBlood.progress+=0.01;
        }.bind(this),0.01,100)
        this.progress=1;
    },

    start(){
        this.score.string=0;
        // this.updateBomb();
        // this.updateEmp();
        // this.updateZhaohuan();
        this.showSkills(); 
        this.moveProp.active=false;
        this.schedule(this.refershWarn.bind(this),1)
        // global.BannerAD.createBanner()
    },

    refershWarn(){
        if(cc.isPauseGame){
            return
        }
        var time = Math.floor(global.GameLoop.GameMgr.monsterMgr.n_effectTime);
        if(time == 3){
            
            if(global.GameLoop.GameMgr.monsterMgr.n_effectData.id >= 0){
                // this.earlyWarn.node.active = true;
                this.earlyWarn.setToggle(global.GameLoop.GameMgr.monsterMgr.n_effectData.id);
                this.tips.children[0].getComponent('UI-Toggle').setToggle(global.GameLoop.GameMgr.monsterMgr.n_effectData.id);
                if(Math.floor(time) == 3){
                    var self = this.earlyWarn.node
                    var tips=this.tips
                    var waring=this.waring
                    this.scheduleOnce(()=>{
                        var t=global.GameLoop.GameMgr.monsterMgr.n_effectData.id==1?5:global.GameLoop.GameMgr.monsterMgr.n_effectData.id==2?5:global.GameLoop.GameMgr.monsterMgr.n_effectData.id==3?6:global.GameLoop.GameMgr.monsterMgr.n_effectData.id==4?3.5:3;
                        if(global.GameLoop.GameMgr.monsterMgr.n_effectData.id==1 || global.GameLoop.GameMgr.monsterMgr.n_effectData.id==2 || global.GameLoop.GameMgr.monsterMgr.n_effectData.id==3){
                            tips.getComponent(cc.Animation).play('gamefury')
                            waring.active=true;
                            waring.runAction(cc.sequence(cc.blink(t+1,t*2+2),cc.callFunc(()=>{
                                waring.active=false
                            })))
                        }
                    },3)
                    // self.runAction(cc.sequence(cc.blink(3,6),cc.callFunc(function(no){
                    //     self.active = false;
                        
                        
                    // })));
                }
            }
        }

        var label = this.earlyWarn.node.getChildByName('label')
        label.getComponent(cc.Label).string = Math.floor(time);
        // global.GameLoop.GameMgr.monsterMgr.n_effectData && this.earlyWarn.setToggle(global.GameLoop.GameMgr.monsterMgr.n_effectData.id)
        
    },

    propMove(position,type){
        this.moveProp.active=true
        this.moveProp.setPosition(position);
        var proptime=cc.find('PropTime',this.node);
        var y=proptime.y-proptime.height/2
        cc.find('icon',this.moveProp).getComponent(cc.Sprite).spriteFrame=this.farms[type]
        this.moveProp.runAction(cc.sequence(cc.moveTo(0.5,cc.v2(-290,y)),cc.callFunc(()=>{
            this.moveProp.active=false;
        })))
    },

    showBaozou(number){
        this.baozou.active=true;
        cc.find('bar',this.baozou).getComponent(cc.Sprite).fillRange=number;
        if(number>0){
            this.baozou.active=false
        }
    },

    showHudun(number){
        this.hudun.active=true;
        cc.find('bar',this.hudun).getComponent(cc.Sprite).fillRange=number
        if(number>0){
            this.hudun.active=false
        }
    },

    showLiaoji(number){
        this.liaoji.active=true;
        cc.find('bar',this.liaoji).getComponent(cc.Sprite).fillRange=number
        if(number>0){
            this.liaoji.active=false
        }
    },

    showWeili(number){
        this.weili.active=true;
        cc.find('bar',this.weili).getComponent(cc.Sprite).fillRange=number
        if(number>0){
            this.weili.active=false
        }
    },

    showSkills(){
        
        var rc = require('RewardComponent');
        let has = rc.hasVideo();

        this.supCount.string='x'+(2-this.useSup)

        if(!has || this.useSup>=2){
            this.bombBtn.active=false;
        }
    },

    onDestroy(){
        global.Event.off('upBullet');
        global.Event.off('crazyTime');

        global.Event.off('bossBattle');
        global.Event.off('bossBlood');
        global.Event.off('playerBlood');
        global.Event.off('onEventBassExit');
        global.Event.off('LUCK');
        global.Event.off('SHOWBLOOD')
        global.Event.off('bombhint')
        // global.BannerAD.destroy()
        cc.game.off(cc.game.EVENT_HIDE);
        cc.game.off(cc.game.EVENT_SHOW);
    },

    onDisable(){
        this.onDestroy();
    },

    luck(){
        this.scheduleOnce(()=>{
            global.luck=true
        },1)
    },
    updateRunTime(){
        var alltime = global.GameLoop.GameMgr.getAllTime();
        var runTime = global.GameLoop.GameMgr.getRuningTime()
        cc.find('headBg/time',this.node).getComponent(cc.Label).string = '时间:'+Math.floor(runTime)+'/'+Math.floor(alltime)
        
        //每3分钟出现幸运礼包
        /*if(global.gates == 0 && Math.floor(alltime)%180==0 && Math.floor(alltime)>=180 && global.luck){
            cc.log("幸运礼包");
            global.luck=false
            cc.audioEngine.pauseAll();
            global.GameLoop.GameMgr.pauseGame(5);
            global.UIMgr.loadLayer('LuckGift',0);
        }*/

        if(runTime>=8 && runTime<=10){
            global.Event.emit('NOTICE',{type:'1'})
        }else if(runTime>=14 && runTime<=16){
            global.Event.emit('NOTICE',{type:'2'})
        }else if(runTime>=16){
            global.Event.emit('NOTICE',{type:'3'})
        }
    },  

    onEventUpBullet(level){
        // cc.log('onEventUpBullet:',level)
        var energy = this.node.getChildByName('Energy');
        energy.getChildByName('energy1').active = level >=1
        energy.getChildByName('energy2').active = level >=2
        energy.getChildByName('energy3').active = level >=3

        var energy = this.node.getChildByName('Energy');
        var pro = energy.getChildByName('energy4').getComponent(cc.ProgressBar)

        if(level == 4 && pro.progress == 0){
            var node = global.Loader.getInstantiate('game/animate/GameFury')
            this.node.addChild(node)
        }

        energy.getChildByName('energy4').getComponent(cc.ProgressBar).progress = level == 4?1:0
    },
    updateScore(s){
        this.score.string=s;
    },

    onEventCrazyTime(progress){
        var energy = this.node.getChildByName('Energy');
        var pro = energy.getChildByName('energy4').getComponent(cc.ProgressBar)
        pro.progress = progress
    },
    onClickPause(event){
        if(!global.canPuse){
            cc.log('飞机已经死亡，不能暂停')
            return;
        }
        // console.log('声音暂停')
        // cc.audioEngine.pauseAll();
        global.GameLoop.GameMgr.pauseGame(6);
        var pause = global.UIMgr.pushUI('game/GamePause')
        pause.getComponent('GameUI-Pause').setOptions({pause:true})
    },

    showBlood(){
        SoundMgr.playSound('Audio/warning');
        cc.find('bossInfo/progress2',this.node).active=true;
        cc.find('bossInfo/progress1',this.node).active=true;
        this.scheduleOnce(function(){
            SoundMgr.playSound('Audio/boss_come');
        },3)
        this.node.getChildByName('bossInfo').active = true;
        var pbar1  = cc.find('bossInfo/progress1',this.node).getComponent(cc.ProgressBar)
        var pbar2  = cc.find('bossInfo/progress2',this.node).getComponent(cc.ProgressBar)
        var pro = 0;
        this.schedule(function(){
            pbar1.progress =  ++pro / 100;
            pbar2.progress =  ++pro / 100;
        },0.01,100)
    },

    onEventBossBattle(){ //boss出场
        cc.log('boss出场');
        var enter = global.Loader.getInstantiate('game/BossEnterBg');
        this.node.addChild(enter);
        enter.runAction(cc.sequence(cc.delayTime(2.0),cc.callFunc(function(){
            cc.log('boss出场走了一程');
            enter.destroy();
            // if(global.gates !=0&&global.gates !=-1 )global.GameLoop.GameMgr.startPlot('bossstart');
        }.bind(this))))
    },

    onEventUpdateBossBlood(data){
        let progress=data.progress;
        let bloodCount=data.bloodCount
        if(bloodCount==1){
            if(data.isBig){
                cc.find('bossInfo/progress2',this.node).active=true;
                cc.find('bossInfo/progress1',this.node).active=true;
            }else{
                cc.find('bossInfo/progress2',this.node).active=false;
                cc.find('bossInfo/progress1',this.node).active=true;
            }
        }else{
            cc.find('bossInfo/progress2',this.node).active=true;
            cc.find('bossInfo/progress1',this.node).active=false;
        }
        cc.find('bossInfo/progress'+bloodCount,this.node).getComponent(cc.ProgressBar).progress = progress
    },
    onEventBassExit(){
        this.node.getChildByName('bossInfo').active = false;
    },

    onEventUpdatePlayerBlood(data){
        // this.canPuse=this.progress*100-data.data>=0;
        this.schedule(function(){
            if(data.add){
                this.progress+=0.01
            }else{
                this.progress-=0.01
            }
            this.playerBlood.progress=this.progress;
        },0.01,parseInt(data.data))
    },

    onClickEmp(){
        if(global.Data.emp > 0){
            SoundMgr.playSound('Audio/Item_shield');
            global.GameLoop.GameMgr.onReleaseEmp();
            if(global.gates!=-1){
                global.Data.emp --;
                this.updateEmp();
            }
        } else {
            cc.log('护盾不足')
        }
    },
    onClickSuperBomb(){

        if(!global.canPuse){
            cc.log('飞机已经死亡，不能释放大招')
            return;
        }

        this.useSup++;
        let that=this;
        global.GameLoop.GameMgr.pauseGame(6);

        console.log('global.Data.bomb:',global.Data.bomb)
        // this.onClickPause();


        var options={
            adUnitId:'adunit-bcd35c706b62374e',
            stopCb:function(){
                global.EJMgr.showToast('视频未看完，不能获得奖励')
                global.GameLoop.GameMgr.resumeGame();
            },
            successCb:function(){
                if(global.GameLoop.GameMgr){
                    global.GameLoop.GameMgr.resumeGame();
                    global.GameLoop.GameMgr.onReleaseBomb();
                    that.bomb.string = 'x' + (1-that.useSup);
                    that.showSkills();
                }
                
            }
        }
        cc.log('广告按钮被点击');
        global.WechatShare.videoOrShare(options)
        
    },

    
    checkHaveFriendlyLink() {
        let friendlyLinks = [];
        global.Data.friendlyLinks.forEach(element => {
            if (element.sceneName == 'Game') friendlyLinks.push(element);
        });
        for (let i = 0; i < this.friendlyLinksNode.length; i++) {
            this.friendlyLinksNode[i].active = false;
        }
        let nodeName = ['右上', '右下'];
        for (let i = 0; i < friendlyLinks.length; i++) {
            let area = nodeName.indexOf(friendlyLinks[i].area);
            if (area != -1) {
                this.friendlyLinksNode[area].getComponent('FriendlyLink').setOptions(friendlyLinks[i]);
            }
        };
    },

    update (dt) {},
});
